package com.springBoot.springBootSysCore.common.third.pay.alipay;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.springBoot.springBootSysCore.common.utils.DateUtils;


/**
 * 支付宝
 * @author asus
 *
 */
public class Alipay {
	/**
	 * 签约合作者身份ID
	 */
	public static final String PARTNER = "";
	/**
	 * 签约卖家支付宝账号
	 */
	public static final String SELLER_ID = PARTNER;
	/**
	 * pkcs8私钥
	 */
	public static final String PRIVATE_KEY = "/MmXY42jcp59C+1/BXOJBw==";
	/**
	 * 支付宝公钥[注：不是开发者公钥]
	 */
	public static final String ALI_PUBLIC_KEY = "/9qwVfgoUhUYuLkxpLQIDAQAB";
	/**
	 * 编码格式
	 */
	public static final String INPUT_CHARSET = "utf-8";
	/**
	 * 支付接口名称：目前是移动支付
	 */
	public static final String SERVICE = "mobile.securitypay.pay";
	/**
	 * 退款接口名称：目前是即时到账有密退款
	 */
	public static final String REFUND = "refund_fastpay_by_platform_pwd";
	/**
	 * 签名算法类型
	 */
	public static final String SIGN_TYPE = "RSA";
	/**
     * 支付宝提供给商户的服务接入网关URL(新)
     */
    private static final String ALIPAY_GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?";

    
    
	/**
	 * 支付：支付初始化参数
	 * @param number	订单号
	 * @param subject	商品名
	 * @param price		价格
	 * @param notify_url		异步通知路径
	 * @return	原始参数集合
	 */
	public static Map<String,String> initPayParam(String number,String subject,String price,String notify_url){
		Map<String,String> staticMap = new HashMap<String, String>();
		staticMap.put("partner", PARTNER);
		staticMap.put("seller_id", SELLER_ID);
		staticMap.put("out_trade_no", number);
		staticMap.put("subject", subject);
		staticMap.put("body", "123");
		staticMap.put("total_fee", price);
		staticMap.put("service", SERVICE);
		staticMap.put("notify_url", notify_url);
		staticMap.put("payment_type", "1");
		staticMap.put("_input_charset", INPUT_CHARSET);
		staticMap.put("it_b_pay", "30m");
		staticMap.put("return_url", "m.alipay.com");

		return staticMap;
	}
	
    /**
     * APP支付使用：生成要请求的给支付宝的参数数组
     * @param sParaTemp 请求前的参数数组
     * @return 要请求的参数数组
     * @throws UnsupportedEncodingException 
     */
    public static String buildRequestParaAPP(Map<String, String> sParaTemp) throws UnsupportedEncodingException {
    	//除去数组中的空值和签名参数
        Map<String, String> sPara = AssistUtil.paraFilter(sParaTemp);
        //生成签名结果
        String mysign = AssistUtil.buildRequestMysign(sPara);
        
        sParaTemp.put("sign", URLEncoder.encode(mysign, "UTF-8"));
        sParaTemp.put("sign_type", "RSA");
        
    	return AssistUtil.createLinkString(sParaTemp);
    }
    
    /**
	 * 退款：支付初始化参数
	 * @param batch_num		退款笔数
	 * @param detail_data	单笔数据集
	 * @param batch_no		批次号
	 * @param notify_url	异步通知路径
	 * @return	原始参数集合
	 */
	public static Map<String,String> initRefundParam(String batch_num,String detail_data,String batch_no,String notify_url){
		Map<String,String> staticMap = new HashMap<String, String>();
		staticMap.put("service", REFUND);
		staticMap.put("partner", PARTNER);
		staticMap.put("_input_charset", INPUT_CHARSET);
		staticMap.put("notify_url", notify_url);
		staticMap.put("seller_user_id", SELLER_ID);
		staticMap.put("refund_date", DateUtils.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"));
		staticMap.put("batch_no", batch_no);
		//退款笔数：默认为1
		staticMap.put("batch_num", batch_num);
		//单笔数据集
		staticMap.put("detail_data", detail_data);

		return staticMap;
	}
	/**
	 * APP支付使用：支付宝退款
	 * @param paraTemp
	 * @return
	 * @throws Exception
	 */
	public static String refund(Map<String, String> paraTemp) throws Exception{
		//除去数组中的空值和签名参数
        Map<String, String> sPara = AssistUtil.paraFilter(paraTemp);
        //生成签名结果
        String mysign = AssistUtil.buildRequestMysign(sPara);
        
        sPara.put("sign", mysign);
        sPara.put("sign_type", "RSA");
        
        List<String> keys = new ArrayList<String>(sPara.keySet());

        StringBuffer sbHtml = new StringBuffer();

        sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + ALIPAY_GATEWAY_NEW
                      + "_input_charset=" + INPUT_CHARSET + "\" method='POST'>");

        for (int i = 0; i < keys.size(); i++) {
            String name = (String) keys.get(i);
            String value = (String) sPara.get(name);

            sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
        }

        //submit按钮控件请不要含有name属性
        sbHtml.append("<input type=\"submit\" value=\"确认\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms['alipaysubmit'].submit();</script>");

        return sbHtml.toString();
		
	}
	
	/**
     * PC支付使用：生成要请求给支付宝的参数数组
     * @param sParaTemp 请求前的参数数组
     * @return 要请求的参数数组
     */
	public static Map<String, String> buildRequestParaPC(Map<String, String> sParaTemp) {
        //除去数组中的空值和签名参数
        Map<String, String> sPara = AssistUtil.paraFilter(sParaTemp);
        //生成签名结果
        String mysign = AssistUtil.buildRequestMysign(sPara);

        //签名结果与签名方式加入请求提交参数组中
        sPara.put("sign", mysign);
        sPara.put("sign_type", Alipay.SIGN_TYPE);

        return sPara;
    }

}
